package gk.cxd.friday.controller;


import gk.cxd.friday.common.base.BaseController;
import gk.cxd.friday.common.constant.UserConstants;
import gk.cxd.friday.common.result.RestResult;
import gk.cxd.friday.common.result.TableDataInfo;
import gk.cxd.friday.model.vo.SysUserVO;
import gk.cxd.friday.service.SysRoleService;
import gk.cxd.friday.service.impl.SysUserServiceImpl;
import gk.cxd.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user")
public class SysUserContorller extends BaseController {
    @Autowired
    SysUserServiceImpl service;

    @Autowired
    private SysRoleService roleService;

    /**
     * 控制层-实现用户列表
     * @return
     */
    /*@GetMapping("/list")
    public List<SysUser> list(){
        return service.selectSysUserList();
    }*/
    @GetMapping("/list")
    public TableDataInfo list(SysUserVO user, Pageable page){
        int pageNum = page.getPageNumber() -1;
        pageNum = pageNum <=0?0:pageNum;
        page = PageRequest.of(pageNum,page.getPageSize());
        return service.selectSysUserList(user,page);
    }

    /**
     * 控制层-添加用户
     * @return
     */

    @PostMapping
    public RestResult add(@RequestBody SysUserVO user){
        if(UserConstants.NOT_UNIQUE.equals(service.checkUserNameUnique(user.getUserName()))){
            return RestResult.error("新增用户" + user.getUserName() + "失败，登录账号已经存在");
        }else
        if(UserConstants.NOT_UNIQUE.equals(service.checkPhoneUnique(user))){
            return RestResult.error("新增用户" + user.getUserName() + "失败，手机号码已经存在");
        }else
        if(UserConstants.NOT_UNIQUE.equals(service.checkEmailUnique(user))){
            return RestResult.error("新增用户" + user.getUserName() + "失败，邮箱账号已经存在" );
        }
        user.setCreateBy("system");
        boolean flag = service.insertUser(user);
        return toAjax(flag ? 1 : 0);
    }

    @DeleteMapping("/{userIds}")
    public RestResult remove(@PathVariable Long[] userIds){
        return toAjax(service.deleteUserByIds(userIds));
    }

    /**
     * 控制层-更新用户
     */
    @PutMapping
    public RestResult edit(@Validated @RequestBody SysUserVO user){
        if(UserConstants.NOT_UNIQUE.equals(service.checkPhoneUnique(user))){
            return RestResult.error("修改用户"+user.getUserName()+"失败，手机号码已存在");
        }else
        if(UserConstants.NOT_UNIQUE.equals(service.checkEmailUnique(user))){
            return RestResult.error("修改用户"+user.getUserName()+"失败，邮箱账号已经存在");
        }
        user.setUpdateBy("system");
        boolean flag = service.updateUser(user);
        return toAjax(flag?1:0);
    }

    /**
     * 根据用户id获取详细信息
     * @param userId
     * @return
     */
    @GetMapping(value = {"/", "/{userId}"})
    public RestResult getInfo(@PathVariable(value = "userId",required = false)Long userId) {
        RestResult ajax = RestResult.success();
        ajax.put("role", roleService.selectRoleAll());
        if (StringUtils.isNotNull(userId)) {
            ajax.put(RestResult.DATA_TAG, service.selectUserId(userId));
            ajax.put("roleIds", roleService.selectRoleListByUserId(userId));
        }
        return ajax;
    }
}


